<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="../">
<head>
  <meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>json_lines &mdash; The Logtalk Handbook v3.93.0-b01 documentation</title>
      <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=d75fae25" />
      <link rel="stylesheet" type="text/css" href="../_static/css/theme.css?v=19f00094" />
      <link rel="stylesheet" type="text/css" href="../_static/css/custom.css?v=396eccfe" />

  
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
        <script src="../_static/jquery.js?v=5d32c60e"></script>
        <script src="../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
        <script src="../_static/documentation_options.js?v=c8100655"></script>
        <script src="../_static/doctools.js?v=9a2dae69"></script>
        <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
    <script src="../_static/js/theme.js"></script>
    <!-- begin favicon -->
    <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
    <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />
    <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
    <link rel="manifest" href="/site.webmanifest" />
    <link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5" />
    <meta name="msapplication-TileColor" content="#355b95" />
    <meta name="theme-color" content="#ffffff" />
    <!-- end favicon -->
    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="listing" href="listing.html" />
    <link rel="prev" title="json" href="json.html" />
   
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >

          
          
          <a href="../index.html" class="icon icon-home">
            The Logtalk Handbook
              <img src="../_static/logtalk.gif" class="logo" alt="Logo"/>
          </a>
              <div class="version">
                3.93.0
              </div>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
    
              <p class="caption" role="heading"><span class="caption-text">Contents</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../userman/index.html">User Manual</a></li>
<li class="toctree-l1"><a class="reference internal" href="../refman/index.html">Reference Manual</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../faq/index.html">FAQ</a></li>
<li class="toctree-l1"><a class="reference internal" href="../devtools/index.html">Developer Tools</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Libraries</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="overview.html">Overview</a></li>
<li class="toctree-l2"><a class="reference internal" href="arbitrary.html"><code class="docutils literal notranslate"><span class="pre">arbitrary</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="assignvars.html"><code class="docutils literal notranslate"><span class="pre">assignvars</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="base64.html"><code class="docutils literal notranslate"><span class="pre">base64</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="basic_types.html"><code class="docutils literal notranslate"><span class="pre">basic_types</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="cbor.html"><code class="docutils literal notranslate"><span class="pre">cbor</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="core.html"><code class="docutils literal notranslate"><span class="pre">core</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="coroutining.html"><code class="docutils literal notranslate"><span class="pre">coroutining</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="csv.html"><code class="docutils literal notranslate"><span class="pre">csv</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="dates.html"><code class="docutils literal notranslate"><span class="pre">dates</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="dependents.html"><code class="docutils literal notranslate"><span class="pre">dependents</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="dictionaries.html"><code class="docutils literal notranslate"><span class="pre">dictionaries</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="dif.html"><code class="docutils literal notranslate"><span class="pre">dif</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="edcg.html"><code class="docutils literal notranslate"><span class="pre">edcg</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="events.html"><code class="docutils literal notranslate"><span class="pre">events</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="expand_library_alias_paths.html"><code class="docutils literal notranslate"><span class="pre">expand_library_alias_paths</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="expecteds.html"><code class="docutils literal notranslate"><span class="pre">expecteds</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="format.html"><code class="docutils literal notranslate"><span class="pre">format</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="genint.html"><code class="docutils literal notranslate"><span class="pre">genint</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="gensym.html"><code class="docutils literal notranslate"><span class="pre">gensym</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="git.html"><code class="docutils literal notranslate"><span class="pre">git</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="grammars.html"><code class="docutils literal notranslate"><span class="pre">grammars</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="heaps.html"><code class="docutils literal notranslate"><span class="pre">heaps</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="hierarchies.html"><code class="docutils literal notranslate"><span class="pre">hierarchies</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="hook_flows.html"><code class="docutils literal notranslate"><span class="pre">hook_flows</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="hook_objects.html"><code class="docutils literal notranslate"><span class="pre">hook_objects</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="html.html"><code class="docutils literal notranslate"><span class="pre">html</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="ids.html"><code class="docutils literal notranslate"><span class="pre">ids</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="intervals.html"><code class="docutils literal notranslate"><span class="pre">intervals</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="java.html"><code class="docutils literal notranslate"><span class="pre">java</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="json.html"><code class="docutils literal notranslate"><span class="pre">json</span></code></a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#"><code class="docutils literal notranslate"><span class="pre">json_lines</span></code></a><ul>
<li class="toctree-l3"><a class="reference internal" href="#api-documentation">API documentation</a></li>
<li class="toctree-l3"><a class="reference internal" href="#loading">Loading</a></li>
<li class="toctree-l3"><a class="reference internal" href="#testing">Testing</a></li>
<li class="toctree-l3"><a class="reference internal" href="#representation">Representation</a></li>
<li class="toctree-l3"><a class="reference internal" href="#encoding">Encoding</a></li>
<li class="toctree-l3"><a class="reference internal" href="#decoding">Decoding</a></li>
<li class="toctree-l3"><a class="reference internal" href="#known-issues">Known issues</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="listing.html"><code class="docutils literal notranslate"><span class="pre">listing</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="logging.html"><code class="docutils literal notranslate"><span class="pre">logging</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="loops.html"><code class="docutils literal notranslate"><span class="pre">loops</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="meta.html"><code class="docutils literal notranslate"><span class="pre">meta</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="meta_compiler.html"><code class="docutils literal notranslate"><span class="pre">meta_compiler</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="mutations.html"><code class="docutils literal notranslate"><span class="pre">mutations</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="nested_dictionaries.html"><code class="docutils literal notranslate"><span class="pre">nested_dictionaries</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="optionals.html"><code class="docutils literal notranslate"><span class="pre">optionals</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="options.html"><code class="docutils literal notranslate"><span class="pre">options</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="os.html"><code class="docutils literal notranslate"><span class="pre">os</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="queues.html"><code class="docutils literal notranslate"><span class="pre">queues</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="random.html"><code class="docutils literal notranslate"><span class="pre">random</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="reader.html"><code class="docutils literal notranslate"><span class="pre">reader</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="recorded_database.html"><code class="docutils literal notranslate"><span class="pre">recorded_database</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="redis.html"><code class="docutils literal notranslate"><span class="pre">redis</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="sets.html"><code class="docutils literal notranslate"><span class="pre">sets</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="statistics.html"><code class="docutils literal notranslate"><span class="pre">statistics</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="term_io.html"><code class="docutils literal notranslate"><span class="pre">term_io</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="timeout.html"><code class="docutils literal notranslate"><span class="pre">timeout</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="tsv.html"><code class="docutils literal notranslate"><span class="pre">tsv</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="types.html"><code class="docutils literal notranslate"><span class="pre">types</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="ulid.html"><code class="docutils literal notranslate"><span class="pre">ulid</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="unicode_data.html"><code class="docutils literal notranslate"><span class="pre">unicode_data</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="union_find.html"><code class="docutils literal notranslate"><span class="pre">union_find</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="uuid.html"><code class="docutils literal notranslate"><span class="pre">uuid</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="zippers.html"><code class="docutils literal notranslate"><span class="pre">zippers</span></code></a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../ports/index.html">Ports</a></li>
<li class="toctree-l1"><a class="reference internal" href="../contributions/index.html">Contributions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../glossary.html">Glossary</a></li>
<li class="toctree-l1"><a class="reference internal" href="../bibliography.html">Bibliography</a></li>
<li class="toctree-l1"><a class="reference internal" href="../genindex.html">Index</a></li>
</ul>

    <p class="caption"><span class="caption-text">External Contents</span></p>
    <ul>
    <li class="toctree-l1"><a class="reference internal" href="../../apis/index.html">APIs</a></li>
    <li class="toctree-l1"><a class="reference internal" href="https://logtalk.org">Logtalk website</a></li>
    <li class="toctree-l1"><a class="reference internal" href="https://github.com/LogtalkDotOrg/logtalk3">GitHub repo</a></li>
    </ul>
  
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">The Logtalk Handbook</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
          <li class="breadcrumb-item"><a href="index.html">Libraries</a></li>
      <li class="breadcrumb-item active"><code class="docutils literal notranslate"><span class="pre">json_lines</span></code></li>
      <li class="wy-breadcrumbs-aside">
              <a href="https://github.com/LogtalkDotOrg/logtalk3/blob/master/docs/handbook/sources/libraries/json_lines.rst" class="fa fa-github"> Edit on GitHub</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <section id="json-lines">
<span id="library-json-lines"></span><h1><code class="docutils literal notranslate"><span class="pre">json_lines</span></code><a class="headerlink" href="#json-lines" title="Link to this heading"></a></h1>
<p>The <code class="docutils literal notranslate"><span class="pre">json_lines</span></code> library provides predicates for parsing and
generating data in the JSON Lines format based on the proposal found at:</p>
<p><a class="reference external" href="https://jsonlines.org">https://jsonlines.org</a></p>
<p>It includes parametric objects whose parameters allow selecting the
representation for parsed JSON objects (<code class="docutils literal notranslate"><span class="pre">curly</span></code> or <code class="docutils literal notranslate"><span class="pre">list</span></code>), JSON
text strings (<code class="docutils literal notranslate"><span class="pre">atom</span></code>, <code class="docutils literal notranslate"><span class="pre">chars</span></code>, or <code class="docutils literal notranslate"><span class="pre">codes</span></code>) and JSON pairs
(<code class="docutils literal notranslate"><span class="pre">dash</span></code>, <code class="docutils literal notranslate"><span class="pre">equal</span></code>, or <code class="docutils literal notranslate"><span class="pre">colon</span></code>).</p>
<section id="api-documentation">
<h2>API documentation<a class="headerlink" href="#api-documentation" title="Link to this heading"></a></h2>
<p>Open the
<a class="reference external" href="../../apis/library_index.html#json_lines">../../apis/library_index.html#json_lines</a>
link in a web browser.</p>
</section>
<section id="loading">
<h2>Loading<a class="headerlink" href="#loading" title="Link to this heading"></a></h2>
<p>To load all entities in this library, load the <code class="docutils literal notranslate"><span class="pre">loader.lgt</span></code> file:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span>| <span class="o">?-</span> <span class="k">logtalk_load</span>(json_lines(loader)).
</pre></div>
</div>
</section>
<section id="testing">
<h2>Testing<a class="headerlink" href="#testing" title="Link to this heading"></a></h2>
<p>To test this library predicates, load the <code class="docutils literal notranslate"><span class="pre">tester.lgt</span></code> file:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span>| <span class="o">?-</span> <span class="k">logtalk_load</span>(json_lines(tester)).
</pre></div>
</div>
<p>Some of the sample JSON test files are based on examples published at:</p>
<p><a class="reference external" href="https://jsonlines.org/">https://jsonlines.org/</a></p>
</section>
<section id="representation">
<h2>Representation<a class="headerlink" href="#representation" title="Link to this heading"></a></h2>
<p>The following choices of syntax have been made to represent JSON
elements as terms:</p>
<ul>
<li><p>By default, JSON objects are represented using curly-bracketed terms,
<code class="docutils literal notranslate"><span class="pre">{Pairs}</span></code>, where each pair uses the representation <code class="docutils literal notranslate"><span class="pre">Key-Value</span></code>
(see below for alternative representations).</p></li>
<li><p>Arrays are represented using lists.</p></li>
<li><p>Text strings can be represented as atoms, <code class="docutils literal notranslate"><span class="pre">chars(List)</span></code>, or
<code class="docutils literal notranslate"><span class="pre">codes(List)</span></code>. The default when decoding is to use atoms when using
the <code class="docutils literal notranslate"><span class="pre">json_lines</span></code> object. To decode text strings into lists of chars
or codes, use the <code class="docutils literal notranslate"><span class="pre">json_lines/1</span></code> object with the parameter bound to
<code class="docutils literal notranslate"><span class="pre">chars</span></code> or <code class="docutils literal notranslate"><span class="pre">codes</span></code>. For example:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span>| <span class="o">?-</span> json_lines<span class="o">::</span>parse(codes([<span class="m">34</span>,<span class="m">104</span>,<span class="m">101</span>,<span class="m">108</span>,<span class="m">108</span>,<span class="m">111</span>,<span class="m">34</span>]), <span class="nv">Terms</span>).
<span class="nv">Terms</span> <span class="o">=</span> [hello]
yes

| <span class="o">?-</span> json_lines(atom)<span class="o">::</span>parse(codes([<span class="m">34</span>,<span class="m">104</span>,<span class="m">101</span>,<span class="m">108</span>,<span class="m">108</span>,<span class="m">111</span>,<span class="m">34</span>]), <span class="nv">Terms</span>).
<span class="nv">Terms</span> <span class="o">=</span> [hello]
yes

| <span class="o">?-</span> json_lines(chars)<span class="o">::</span>parse(codes([<span class="m">34</span>,<span class="m">104</span>,<span class="m">101</span>,<span class="m">108</span>,<span class="m">108</span>,<span class="m">111</span>,<span class="m">34</span>]), <span class="nv">Terms</span>).
<span class="nv">Terms</span> <span class="o">=</span> [chars([h,<span class="o">e</span>,l,l,o])]
yes

| <span class="o">?-</span> json_lines(codes)<span class="o">::</span>parse(codes([<span class="m">34</span>,<span class="m">104</span>,<span class="m">101</span>,<span class="m">108</span>,<span class="m">108</span>,<span class="m">111</span>,<span class="m">34</span>]), <span class="nv">Terms</span>).
<span class="nv">Terms</span> <span class="o">=</span> [codes([<span class="m">104</span>,<span class="m">101</span>,<span class="m">108</span>,<span class="m">108</span>,<span class="m">111</span>])]
yes
</pre></div>
</div>
</li>
<li><p>The JSON values <code class="docutils literal notranslate"><span class="pre">false</span></code>, <code class="docutils literal notranslate"><span class="pre">true</span></code> and <code class="docutils literal notranslate"><span class="pre">null</span></code> are represented by,
respectively, the <code class="docutils literal notranslate"><span class="pre">&#64;false</span></code>, <code class="docutils literal notranslate"><span class="pre">&#64;true</span></code> and <code class="docutils literal notranslate"><span class="pre">&#64;null</span></code> compound terms.</p></li>
</ul>
<p>The following table exemplifies the term equivalents of JSON elements
using default representations for objects, pairs, and strings:</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>JSON</p></th>
<th class="head"><p>term</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>[1,2]</p></td>
<td><p>[1,2]</p></td>
</tr>
<tr class="row-odd"><td><p>true</p></td>
<td><p>&#64;true</p></td>
</tr>
<tr class="row-even"><td><p>false</p></td>
<td><p>&#64;false</p></td>
</tr>
<tr class="row-odd"><td><p>null</p></td>
<td><p>&#64;null</p></td>
</tr>
<tr class="row-even"><td><p>-1</p></td>
<td><p>-1</p></td>
</tr>
<tr class="row-odd"><td><p>[1.2345]</p></td>
<td><p>[1.2345]</p></td>
</tr>
<tr class="row-even"><td><p>[]</p></td>
<td><p>[]</p></td>
</tr>
<tr class="row-odd"><td><p>[2147483647]</p></td>
<td><p>[2147483647]</p></td>
</tr>
<tr class="row-even"><td><p>[0]</p></td>
<td><p>[0]</p></td>
</tr>
<tr class="row-odd"><td><p>[1234567890123456789]</p></td>
<td><p>[1234567890123456789]</p></td>
</tr>
<tr class="row-even"><td><p>[false]</p></td>
<td><p>[&#64;false]</p></td>
</tr>
<tr class="row-odd"><td><p>[-2147483648]</p></td>
<td><p>[-2147483648]</p></td>
</tr>
<tr class="row-even"><td><p>{“a”:null,”foo”:”bar”}</p></td>
<td><p><a class="reference external" href="mailto:{a-&#37;&#52;&#48;null">{a-<span>&#64;</span>null</a>, foo-bar}</p></td>
</tr>
<tr class="row-odd"><td><p>[2.225073858507201e-308]</p></td>
<td><p>[2.225073858507201e-308]</p></td>
</tr>
<tr class="row-even"><td><p>[0,1]</p></td>
<td><p>[0,1]</p></td>
</tr>
<tr class="row-odd"><td><p>[2.2250738585072014e-308]</p></td>
<td><p>[2.2250738585072014e-308]</p></td>
</tr>
<tr class="row-even"><td><p>[1.7976931348623157e+308]</p></td>
<td><p>[1.7976931348623157e+308]</p></td>
</tr>
<tr class="row-odd"><td><p>[0.0]</p></td>
<td><p>[0.0]</p></td>
</tr>
<tr class="row-even"><td><p>[4294967295]</p></td>
<td><p>[4294967295]</p></td>
</tr>
<tr class="row-odd"><td><p>[-1234567890123456789]</p></td>
<td><p>[-1234567890123456789]</p></td>
</tr>
<tr class="row-even"><td><p>[“foo”]</p></td>
<td><p>[foo]</p></td>
</tr>
<tr class="row-odd"><td><p>[1]</p></td>
<td><p>[1]</p></td>
</tr>
<tr class="row-even"><td><p>[null]</p></td>
<td><p>[&#64;null]</p></td>
</tr>
<tr class="row-odd"><td><p>[-1.2345]</p></td>
<td><p>[-1.2345]</p></td>
</tr>
<tr class="row-even"><td><p>[5.0e-324]</p></td>
<td><p>[5.0e-324]</p></td>
</tr>
<tr class="row-odd"><td><p>[-1]</p></td>
<td><p>[-1]</p></td>
</tr>
<tr class="row-even"><td><p>[true]</p></td>
<td><p>[&#64;true]</p></td>
</tr>
<tr class="row-odd"><td><p>[9223372036854775807]</p></td>
<td><p>[9223372036854775807]</p></td>
</tr>
</tbody>
</table>
<p>For JSON objects that are two possible term representations:</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>JSON object</p></th>
<th class="head"><p>term (curly)</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>{“a”:1, “b”:2, “c”:3}</p></td>
<td><p>{a-1, b-2, c-3}</p></td>
</tr>
<tr class="row-odd"><td><p>{}</p></td>
<td><p>{}</p></td>
</tr>
</tbody>
</table>
<p>and:</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>JSON object</p></th>
<th class="head"><p>term (list)</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>{“a”:1, “b”:2, “c”:3}</p></td>
<td><p>json([a-1, b-2, c-3])</p></td>
</tr>
<tr class="row-odd"><td><p>{}</p></td>
<td><p>json([])</p></td>
</tr>
</tbody>
</table>
<p>For JSON pairs that are three possible representations:</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>JSON object</p></th>
<th class="head"><p>term (dash)</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>{“a”:1, “b”:2, “c”:3}</p></td>
<td><p>{a-1, b-2, c-3}</p></td>
</tr>
</tbody>
</table>
<p>and:</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>JSON object</p></th>
<th class="head"><p>term (equal)</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>{“a”:1, “b”:2, “c”:3}</p></td>
<td><p>{a=1, b=2, c=3}</p></td>
</tr>
</tbody>
</table>
<p>and:</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>JSON object</p></th>
<th class="head"><p>term (colon)</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>{“a”:1, “b”:2, “c”:3}</p></td>
<td><p>{a:1, b:2, c:3}</p></td>
</tr>
</tbody>
</table>
<p>By default, the curly-term representation and the dash pair
representation are used. The <code class="docutils literal notranslate"><span class="pre">json/3</span></code> parametric object allows
selecting the desired representation choices. For example:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span>| <span class="o">?-</span> json_lines(curly,dash,atom)<span class="o">::</span>parse(<span class="k">atom</span>(<span class="s">&#39;{&quot;a&quot;:1, &quot;b&quot;:2, &quot;c&quot;:3}&#39;</span>), <span class="nv">JSONL</span>).
<span class="nv">JSONL</span> <span class="o">=</span> [<span class="k">{</span>a<span class="o">-</span><span class="m">1</span>, b<span class="o">-</span><span class="m">2</span>, c<span class="o">-</span><span class="m">3</span><span class="k">}</span>]
yes

| <span class="o">?-</span> json_lines(list,equal,atom)<span class="o">::</span>parse(<span class="k">atom</span>(<span class="s">&#39;{&quot;a&quot;:1, &quot;b&quot;:2, &quot;c&quot;:3}&#39;</span>), <span class="nv">JSONL</span>).
<span class="nv">JSONL</span> <span class="o">=</span> [json([a<span class="o">=</span><span class="m">1</span>, b<span class="o">=</span><span class="m">2</span>, c<span class="o">=</span><span class="m">3</span>])]
yes

| <span class="o">?-</span> json_lines(curly,colon,atom)<span class="o">::</span>parse(<span class="k">atom</span>(<span class="s">&#39;{&quot;a&quot;:1, &quot;b&quot;:2, &quot;c&quot;:3}&#39;</span>), <span class="nv">JSONL</span>).
<span class="nv">JSONL</span> <span class="o">=</span> [<span class="k">{</span>a<span class="o">:</span><span class="m">1</span>, b<span class="o">:</span><span class="m">2</span>, c<span class="o">:</span><span class="m">3</span><span class="k">}</span>]
yes
</pre></div>
</div>
</section>
<section id="encoding">
<h2>Encoding<a class="headerlink" href="#encoding" title="Link to this heading"></a></h2>
<p>Encoding is accomplished using the <code class="docutils literal notranslate"><span class="pre">generate/2</span></code> predicate. For
example:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span>| <span class="o">?-</span> json_lines<span class="o">::</span>generate(codes(<span class="nv">Encoding</span>), [a,<span class="k">{</span>b<span class="o">-</span>c<span class="k">}</span>]).
<span class="nv">Encoding</span> <span class="o">=</span> [<span class="m">34</span>,<span class="m">97</span>,<span class="m">34</span>,<span class="m">10</span>,<span class="m">123</span>,<span class="m">34</span>,<span class="m">98</span>,<span class="m">34</span>,<span class="m">58</span>,<span class="m">34</span>,<span class="m">99</span>,<span class="m">34</span>,<span class="m">125</span>,<span class="m">10</span>]
yes
</pre></div>
</div>
<p>Alternatively:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span>| <span class="o">?-</span> json_lines<span class="o">::</span>generate(chars(<span class="nv">Encoding</span>), [a,<span class="k">{</span>b<span class="o">-</span>c<span class="k">}</span>]).
<span class="nv">Encoding</span> <span class="o">=</span> [<span class="s">&#39;&quot;&#39;</span>,a,<span class="s">&#39;&quot;&#39;</span>,<span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span>,<span class="s">&#39;{&#39;</span>,<span class="s">&#39;&quot;&#39;</span>,b,<span class="s">&#39;&quot;&#39;</span>,<span class="o">:</span>,<span class="s">&#39;&quot;&#39;</span>,c,<span class="s">&#39;&quot;&#39;</span>,<span class="s">&#39;}&#39;</span>,<span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span>]
<span class="nv">Yes</span>

| <span class="o">?-</span> json_lines<span class="o">::</span>generate(<span class="k">atom</span>(<span class="nv">Encoding</span>), [a,<span class="k">{</span>b<span class="o">-</span>c<span class="k">}</span>]).
<span class="nv">Encoding</span> <span class="o">=</span> <span class="s">&#39;&quot;a&quot;</span><span class="se">\n</span><span class="s">{&quot;b&quot;:&quot;c&quot;}</span><span class="se">\n</span><span class="s">&#39;</span>
<span class="nv">Yes</span>
</pre></div>
</div>
<p>Notice that <code class="docutils literal notranslate"><span class="pre">generate/2</span></code> takes, as second argument, a Prolog term that
corresponds to the JSON syntax in Prolog and produces the corresponding
JSON output in the format specified in the first argument:
(<code class="docutils literal notranslate"><span class="pre">codes(Variable)</span></code>, <code class="docutils literal notranslate"><span class="pre">stream(Stream)</span></code>, <code class="docutils literal notranslate"><span class="pre">file(File)</span></code>,
<code class="docutils literal notranslate"><span class="pre">chars(Variable)</span></code> or <code class="docutils literal notranslate"><span class="pre">atom(Variable)</span></code>).</p>
</section>
<section id="decoding">
<h2>Decoding<a class="headerlink" href="#decoding" title="Link to this heading"></a></h2>
<p>Decoding is accomplished using the <code class="docutils literal notranslate"><span class="pre">parse/2</span></code> predicate. For example,
to decode a given json file:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span>| <span class="o">?-</span> json_lines<span class="o">::</span>parse(file(<span class="s">&#39;simple/data.jsonl&#39;</span>), <span class="nv">Terms</span>).
<span class="nv">Term</span> <span class="o">=</span> [<span class="k">{</span>a<span class="o">-</span>[b]<span class="k">}</span>]
yes
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">parse/2</span></code> predicate first argument must indicate the input source
(<code class="docutils literal notranslate"><span class="pre">codes(Codes)</span></code>, <code class="docutils literal notranslate"><span class="pre">stream(Stream)</span></code>, <code class="docutils literal notranslate"><span class="pre">line(Stream)</span></code>, <code class="docutils literal notranslate"><span class="pre">file(Path)</span></code>,
<code class="docutils literal notranslate"><span class="pre">chars(Chars)</span></code> or <code class="docutils literal notranslate"><span class="pre">atom(Atom)</span></code>) containing a JSON payload to be
decoded into the Prolog term in the second argument.</p>
</section>
<section id="known-issues">
<h2>Known issues<a class="headerlink" href="#known-issues" title="Link to this heading"></a></h2>
<p>Some tests may fail on backends such as ECLiPSe and GNU Prolog that
don’t support Unicode.</p>
</section>
</section>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="json.html" class="btn btn-neutral float-left" title="json" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="listing.html" class="btn btn-neutral float-right" title="listing" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 1998-2025, Paulo Moura.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>